//引入express模块
const express = require('express');
//引入连接池模块
const pool = require('../pool.js');
//console.log(pool);
//创建路由器对象
const router = express.Router();

//首页轮播图接口 
router.get('/carousel',(req,res) => {
   //获取id参数
  //  let id = req.query.id;
   let sql = "select cid,img,href from se_second_carousel ";
   pool.query(sql,(error,results) => {
     if(error) throw error;
	 //返回数据到客户端
	 res.send({msg:'ok',code:200,data:results});
   }
)});

//书城页书籍展示列表接口 
router.get('/list', (req,res) => {
  //获取地址栏中的id参数
  //let id = req.query.id; 
  let sql = "select bid,title,pic,price,href,orderNumber from se_second_books";
  // 执行SQL查询
  pool.query(sql,(error, results) => {
    if (error) throw error;
    // 返回数据到客户端
    res.send({ msg: 'ok', code: 200, data: results});
  });
});

//书城页书籍详情页展示列表接口 
router.get('/details', (req,res) => {
  //获取地址栏中的id参数
  //let id = req.query.id; 
  let id = req.params.id;
  // SQL查询
  let sql = "SELECT r.bid,pic1,title,price,express,orderNumber,pic2,href FROM se_second_books_details AS r INNER JOIN se_second_books AS u ON se_second_books_bid = u.bid WHERE r.bid limit = 1";

  // 执行SQL查询
  pool.query(sql, [id], (error, results) => {
    if (error) throw error;
    // 返回数据到客户端
    res.send({ message: 'ok', code: 200, result: results[0] });
  });
});

//书籍购买页
router.get('/bookdetail/:bid',(req,res)=>{
	//获取地址栏中的id参数
    let bid = req.params.bid
	console.log(req.params)
	let sql = "select * from se_second_books_details where bid=?"
	pool.query(sql,[bid],(error,result)=>{
	if(error) throw error;
	if(result.length>0){
		res.send(result);
	}else{
		res.send("0")
	}
	})
}) 

//导出路由器对象
module.exports = router;